﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MyBlog.DataAccess;
using MyBlog.Model;
using MyBlog.Common;
using AutoMapper;
using MyBlog.BusinessInterface;
using MyBlog.DataEntity;
using MyBlog.DataAccessInterface;
namespace MyBlog.Business
{
    public class BusinessConcern : IBusinessConcern
    {
        IDALConcern _dalConcern;
        IUnitOfWork _unitWork;
        public BusinessConcern(IDALConcern dalConcern, IUnitOfWork unitWork)
        {
            _dalConcern = dalConcern;
            _unitWork = unitWork;
        }
        public void Add(ConcernModel model)
        {
            var dataEntity = Mapper.Map<Tbl_Concerns>(model);
            _dalConcern.Add(dataEntity);
            _unitWork.Commit();
            model.ID = dataEntity.ID;
        }

        public void Update(ConcernModel model)
        {
            var dataEntity = Mapper.Map<Tbl_Concerns>(model);
            _dalConcern.Update(dataEntity);
            _unitWork.Commit();
        }
        public void AddRange(IEnumerable<ConcernModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_Concerns>>(models);
            _dalConcern.AddRange(dataEntitys);
            _unitWork.Commit();
        }

        public void DeleteRange(IEnumerable<ConcernModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_Concerns>>(models);
            _dalConcern.DeleteRange(dataEntitys);
            _unitWork.Commit();
        }

        public List<ConcernModel> GetAll(bool isTracking = true)
        {
            var entities = _dalConcern.GetAll(isTracking);
            return Mapper.Map<List<ConcernModel>>(entities);
        }

        public void Delete(params object[] primaryKey)
        {
            _dalConcern.Delete(primaryKey);
            _unitWork.Commit();
        }

        public ConcernModel Get(params object[] primaryKey)
        {
            var dataEntity = _dalConcern.Get(primaryKey);
            return Mapper.Map<ConcernModel>(dataEntity);
        }

        public void Delete(ConcernModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Concerns>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            _dalConcern.Delete(where);
            _unitWork.Commit();
        }

        public ConcernModel Get(ConcernModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Concerns>();
            if (model.ID != 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            if (!string.IsNullOrWhiteSpace(model.UID))
            {
                where = where.And(x => x.UID == model.UID);
            }
            if (!string.IsNullOrWhiteSpace(model.ConcernUID))
            {
                where = where.And(x => x.ConcernUID == model.ConcernUID);
            }
            var dataEntity = _dalConcern.Get(where);
            return Mapper.Map<ConcernModel>(dataEntity);
        }

        public List<ConcernModel> GetAll(ConcernModel model, bool isTracking = true)
        {
            var where = LinqExpressionBuilder.True<Tbl_Concerns>();
            var entities = _dalConcern.GetAll(where, isTracking);
            return Mapper.Map<List<ConcernModel>>(entities);
        }


        public List<ConcernModel> GetAll(ConcernModel model, int page, int size, out int total, bool isTracking = true)
        {
            throw new NotImplementedException();
        }
    }
}
